#include "BComboxDelegate.h"
#include <QComboBox>



BComboxDelegate::BComboxDelegate(QObject *parent) :
    QStyledItemDelegate(parent)
{

}

void BComboxDelegate::setItems(QStringList items, bool isEdit)
{
    m_list = items;
    m_isEditable = isEdit;
}
//创建delegate
QWidget *BComboxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    Q_UNUSED(option);
    Q_UNUSED(index);
    QComboBox* combox = new QComboBox(parent);
    for (int i = 0; i < m_list.size(); i++) {
        combox->addItem(m_list.at(i));
    }
    combox->setEditable(m_isEditable);
    combox->setFrame(true);
    return combox;
}
//将数据从model加载到delegate
void BComboxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
    QString value = index.model()->data(index, Qt::EditRole).toString();
    QComboBox* combox = static_cast<QComboBox*>(editor);
    combox->setCurrentText(value);
}
//将数据从delegate加载到model
void BComboxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
    QComboBox* combox = static_cast<QComboBox*>(editor);
    model->setData(index, combox->currentText(), Qt::EditRole);
}
//设置delegate大小
void BComboxDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    Q_UNUSED(index);
    editor->setGeometry(option.rect);
}


